package com.yanqingshan.platform.config;

import cn.hutool.extra.spring.SpringUtil;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: Knife4j Swagger 配置
 *
 * @author yanqs
 * @since 2023-05-12
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    private final OpenApiExtensionResolver openApiExtensionResolver;
    @Value("${knife4j.openapi.title:API查看器}")
    String title;
    @Value("${knife4j.openapi.concat:yanqingshan}")
    String contactName;
    @Value("${knife4j.openapi.url:www.yanqingshan.com}")
    String contactUrl;
    @Value("${knife4j.openapi.email:i@yanqingshan.com}")
    String contactMail;
    @Value("${knife4j.openapi.description:API服务的说明，请在配置文件中说明服务的作用}")
    String description;
    @Value("${knife4j.openapi.version:0.0.0}")
    String version;

    @Autowired
    public Knife4jConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean(value = "defaultApi")
    public Docket defaultApi() {
        String serverPort = SpringUtil.getProperty("server.port");
        List<Parameter> pars = new ArrayList<>();
        // header中的ticket参数非必填，传空也可以
        ParameterBuilder ticketPar = new ParameterBuilder();
        ticketPar.name("authorization").description("authorization").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        //Sign
        ParameterBuilder signTickPar = new ParameterBuilder();
        signTickPar.name("sign").description("sign").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        //Timestamp
        ParameterBuilder timesTickPar = new ParameterBuilder();
        timesTickPar.name("timestamp").description("timestamp").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        pars.add(ticketPar.build());
        pars.add(signTickPar.build());
        pars.add(timesTickPar.build());
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(title)
                        .description(description)
                        .termsOfServiceUrl("http://127.0.0.1:" + serverPort + "/doc.html")
                        .contact(new Contact(contactName, contactUrl, contactMail))
                        .version(version)
                        .build())
                //.enable(flag)
                //分组名称
                .groupName("后端接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.yanqingshan.platform.module"))
                .paths(PathSelectors.any())
                .build()
                //扩展类
                .extensions(openApiExtensionResolver.buildSettingExtensions());
                //.globalRequestParameters(getGlobalRequestParameters())

                //.globalOperationParameters(pars);
        return docket;
    }
}
